package 
{
	import flash.display.DisplayObject;
	import flash.display.MorphShape;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;

	/**
	 * ...
	 * @author Miles Aurbeck
	 */
	public class eIsoPaper_Grid extends Sprite 
	{
		public var triangleLength:Number = 75;
		public var triangleHeight:Number = 0;
		public var startY:int = triangleHeight;
		public var startX:int = triangleLength;
		public var gridMC:MovieClip = new MovieClip();
		public var triArray:Array = new Array();
		public var faceArray:Array = new Array();
		public var top:MovieClip = new MovieClip();
		public var faceMC:MovieClip = new MovieClip();
		public var faceCt:int = 0;
		public var gridLineWidth:int = 0;
		public var faceLineWidth:int = 2;
		public var faceType:String = "top";
		public var faceColor:int = 0x000000;
		
		public function eIsoPaper_Grid():void {		
			
			triangleHeight = (Math.pow(triangleLength, 2) + Math.pow(.5 * triangleLength, 2))
			triangleHeight = Math.sqrt(triangleHeight);
			trace("triHeight: " + triangleHeight);
			
			gridMC.x = -triangleHeight;
			gridMC.y = -triangleHeight;
			
			addChild(gridMC);
			
				for(var j:int = 1; j<=20; j++){
					trace("starting outside loop");
					if(j%2==1)
					startY = triangleHeight;
					else
					startY = triangleHeight - (.5*triangleHeight) - gridLineWidth;
					
					startX = ((triangleLength+gridLineWidth)*j);
					for(var i:int = 1; i<=20; i++){
						var tri:Sprite = new Sprite();
						
						if ((i % 2) == 1) {//if its the first one
							tri = triangleA(gridLineWidth, 0x0000EF, 0x0000CF);
							tri.name = "triA" + String(j) + "a" + String(i);
						}else{//the secone one
							tri = triangleB(gridLineWidth, 0x0000EF, 0x0000CF);
							tri.name = "triB" + String(j) + "a" + String(i - 1);
						}
						
						tri.x =  startX;
						tri.y =  startY;						

						if ((i % 2) == 0) {
							startY += (triangleHeight)+1;
						}
						startY += 1;

						trace("Y: " + (tri.y));
						triArray.push(tri);					
	
						tri.addEventListener(MouseEvent.CLICK, clicked);
						
						gridMC.addChild(tri);
						trace("INFO- x: " + tri.x + ", y: " + tri.y + ", tri.rotation: " + tri.rotation + ", startX: " + startX + ", startY: " + startY);
					}
				}
				gridMC.addChild(faceMC);
					startX = triangleLength;
					startY = triangleHeight;
		}
		
		
				
		public function triangleA(lineWidth:int = 5, fillColor:int = 0x2147AB, lineColor:int = 0x21FFAB):Sprite {
			var triangleShape:Sprite = new Sprite();
			triangleShape.graphics.lineStyle(lineWidth, lineColor);
			triangleShape.graphics.beginFill(fillColor);
			triangleShape.graphics.moveTo(0, 0);
			//triangleShape.graphics.drawCircle(triangleLength, 0,3);
			triangleShape.graphics.lineTo((triangleLength), -(triangleHeight*.5));
			//triangleShape.graphics.lineStyle(lineWidth, 0x000000);
			triangleShape.graphics.lineTo( (triangleLength), triangleHeight*.5);
			//triangleShape.graphics.lineStyle(lineWidth, 0xFF0000);
			triangleShape.graphics.lineTo(0, 0);
			triangleShape.graphics.endFill();
			return(triangleShape);
		}
		
		public function triangleB(lineWidth:int = 5, fillColor:int = 0x2147AB, lineColor:int = 0x21FFAB):Sprite {
			var triangleShape:Sprite = new Sprite();
			triangleShape.graphics.lineStyle(lineWidth, lineColor);
			triangleShape.graphics.beginFill(fillColor);
			triangleShape.graphics.moveTo(0, 0);
			//triangleShape.graphics.drawCircle(triangleLength, 0,3);
			triangleShape.graphics.lineTo( (triangleLength), triangleHeight * .5);
			//triangleShape.graphics.lineStyle(lineWidth, 0x000000);
			triangleShape.graphics.lineTo(0, triangleHeight);
			//triangleShape.graphics.lineStyle(lineWidth, 0xFF0000);
			triangleShape.graphics.lineTo(0, 0);
			triangleShape.graphics.endFill();
			return(triangleShape);
		}
		
		public function clicked(e:MouseEvent):void{
			trace("<clicked>: " );
			trace(e.currentTarget.name);
			trace(e.currentTarget.x);
			trace(e.currentTarget.y);
			trace(e.currentTarget.rotation);
			trace("</clicked>");
			makeFace(e.currentTarget);
		}
		
		
		
		public function makeFace(src:Object):void {
			trace("<addFace>");
			var newFace:MovieClip = new MovieClip();

			var tri:Sprite = triangleB(faceLineWidth,faceColor,faceColor);
			tri.x = 0-triangleLength;
			trace("Y: " + (tri.y = 0));
			
			var tri2:Sprite = triangleA(faceLineWidth,faceColor,faceColor);
			tri2.x = -triangleLength;
			trace("Y: " + (tri2.y = 0));

			newFace.addChild(tri);			
			newFace.addChild(tri2);	
			
			placeFace(newFace, src);
			trace("</addFace>");
		}
		
		public function placeFace(face:MovieClip,src:Object):void {
			trace("<placeFace>");
			faceCt++;			
			
			if(faceType == "right"){
				face.x = src.x+startX;
				face.y = src.y;
				face.rotation = 0;
			}else if (faceType == "left") {
				face.x = src.x + .5 * triangleLength;
				trace(src.name.charAt(3));
				if (src.name.charAt(3) == "A") {//even
					trace("inside");
					face.y = src.y - (.75 * triangleHeight);
					face.rotation = -60;
				}else if (src.name.charAt(3) == "B"){
					face.y = src.y + (.25 * triangleHeight);
					face.rotation = -60;
				}else {
					face.x = src.x;
					face.y = src.y;
				}
			}else if (faceType == "top") {
				if (src.name.charAt(3) == "A") {//even
				face.x = src.x + .5*triangleLength;
				face.y = src.y - .25*triangleHeight;
				face.rotation = -120;
				}else if (src.name.charAt(3) == "B"){
				face.x = src.x - (.5*triangleLength);
				face.y = src.y + (.25*triangleHeight);
				face.rotation = -120;
				}else {
					face.x = src.x;
					face.y = src.y;
				}
			}

			trace("face x: " + face.x);
			trace("face y: " + face.y);
			trace("face rot: " + face.rot);
			
			faceArray.push(face);
			faceMC.addChild(face);
			faceMC.addEventListener(MouseEvent.CLICK, faceDeleteOnClick);
			//faceMC.name = src.name + "_face";
			trace("</placeFace>");
		}
		public function faceDeleteOnClick(evt:MouseEvent):void {
			trace("delete face");
			trace(evt.currentTarget.name);
			//faceMC.removeChild(evt.currentTarget as DisplayObject);
			
			makeFace(evt.currentTarget);
		}		
	}	
}